import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'

const TEST1 = 'http://192.168.1.108/obd/'
const TEST2 = 'http://localhost/obd/'
const PRODUCT = 'https://car.so360.org/obd/'
const HOST = TEST2
const API_LOGIN = 'signIn'
const API_LOGOUT = 'loginOut'
const API_DEALER = 'admin/'
const API_INDEX_DATA = API_DEALER + 'adminIndexView'
const API_ADD_DEALER = API_DEALER + 'dealer'
const API_DELETE_DEALER = API_DEALER + 'dealer/'
const API_UPDATE_DEALER = API_DEALER + 'dealer/'
const API_RESET_PASSWORD = API_DEALER + 'dealer/password/'
const API_UPDATE_PASSWORD = API_DEALER + 'dealer/password/'
const API_RETRIEVE_PASSWORD = 'sendMail'
const API_CHECK_V_CODE = 'checkAuthCode'
const API_FORGET_PASSWORD = 'forgetPassword'
const API_RESET_PHONE = API_DEALER + 'dealer/phone/'
const API_INFO_DEALER = API_DEALER + 'library/user/dealerId/'
const API_SEARCH_EQUIP = API_DEALER + 'library/imei/'//搜设备接口
const API_SEARCH_BY_IMEIS = API_DEALER + 'queryLibraryInImei'//搜设备接口
const API_SEARCH_EQUIP_COO = API_DEALER + 'equipInfo/imei/'//搜设备经纬度
const API_SEARCH_EQUIP_HIS = API_DEALER + 'historyAddress'//历史轨迹
const API_BATCH_MOVE = API_DEALER + 'library/move/batch'//批量转移
const API_SINGLE_MOVE = API_DEALER + 'library/move'//转移
const API_SEARCH_USER = API_DEALER + 'library/user/emailOrUserName' //搜用户接口
const API_SEARCH_USER_ID = API_DEALER + 'library/user/dealerId/'
const API_ADD_DEVICE = API_DEALER + 'library'//添加设备接口
const GET_USER_EQUIP = API_DEALER + 'libraries'//获取客户设备列表接口
const API_EDIT_EQUIP = API_DEALER + 'library' //获取 【编辑】接口
const API_DELETE_EQUIP = API_DEALER + 'library/id/' //获取 【删除】接口
const API_GET_ALL_DEALER = API_DEALER + 'allEquipInfo/superiorId/'//根据总代id查询下属经销商设备信息包括总代
const API_ALL_DEALER = API_DEALER + 'dealer/'
const API_GET_ALARM_PROMPT = API_DEALER + 'alarmPrompt'//报警记录详情
const API_QUERY_ALARM_ID = API_DEALER + 'alarmPrompt/dealerId/' //查询某经销商的某类型报警记录组详情
const API_QUERY_ALARM = API_DEALER + 'alarmPrompt/otherConditions' //报警记录的条件搜索
const API_QUERY_DEALER = API_DEALER + 'alarmPrompt/equipName/superiorId/' //报警记录的条件搜索
const API_EXCEL_ALARM_CONDITION = 'excel/alarmPrompt/otherConditions' //报警记录条件搜索的EXCEL
const API_EXCEL_OFFLINE = 'excel/offlineCountPage/superiorId/' //离线统计的EXCEL
const API_EXCEL_MILEAGE_SUM = 'excel/mileageSum' //里程统计

axios.defaults.baseURL = HOST
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.headers.patch['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.withCredentials = true
axios.interceptors.request.use(function (config) {
    if (config.method !== 'get') {
        config.data = qs.stringify(config.data)
    }
    return config
})

Vue.prototype.$ajax = {
    //登录请求
    autoService: {
        login (params) {
            return axios({
                method: 'POST',
                url: API_LOGIN,
                cache: true,
                data: params
            })
        },
        logout (params) {
            return axios({
                method: 'POST',
                url: API_LOGOUT,
                data: params
            })
        }
    },
    indexData: {
        adminIndexView () {
            return axios({
                method: 'GET',
                url: API_INDEX_DATA
            })
        }
    },
    userInfo: {
        addUser (params) {
            return axios({
                method: 'POST',
                url: API_ADD_DEALER,
                data: params
            })
        },
        updateUser (id, params) {
            return axios({
                method: 'PATCH',
                url: API_UPDATE_DEALER + id,
                data: params
            })
        },
        deleteUser (id) {
            return axios({
                method: 'DELETE',
                url: API_DELETE_DEALER + id,
            })
        },
        resetPwd (id) {
            return axios({
                method: 'DELETE',
                url: API_RESET_PASSWORD + id,
            })
        },
        updatePwd (id, params) {
            return axios({
                method: 'POST',
                url: API_UPDATE_PASSWORD + id,
                data: params
            })
        },
        retrievePwd (params) {
            return axios({
                method: 'POST',
                url: API_RETRIEVE_PASSWORD,
                data: params
            })
        },
        checkVCode (params) {
            return axios({
                method: 'POST',
                url: API_CHECK_V_CODE,
                data: params
            })
        },
        forgetPassword (params) {
            return axios({
                method: 'POST',
                url: API_FORGET_PASSWORD,
                data: params
            })
        },
        resetPhone (id) {
            return axios({
                method: 'DELETE',
                url: API_RESET_PHONE + id,
            })
        },
        getUserInfo (id) {
            return axios({
                method: 'GET',
                url: API_INFO_DEALER + id
            })
        },
        queryUserById (id) {
            return axios({
                method: 'GET',
                url: API_SEARCH_USER_ID + id,
            })
        },
        queryAllDealer (id) {
            return axios({
                method: 'GET',
                url: API_GET_ALL_DEALER + id,
            })
        }
    },
    //设备类
    equip: {  //搜设备请求
        queryEquip (imei) {
            return axios({
                method: 'GET',
                url: API_SEARCH_EQUIP + imei,
            })
        },
        queryEquipByImeis (params) {
            return axios({
                method: 'GET',
                url: API_SEARCH_BY_IMEIS,
                params: params
            })
        },
        queryEquipCoordinate (imei) {
            return axios({
                method: 'GET',
                url: API_SEARCH_EQUIP_COO + imei,
            })
        },
        queryEquipHistory (params) {
            return axios({
                method: 'GET',
                url: API_SEARCH_EQUIP_HIS,
                params: params
            })
        },
        getEquip (params) {
            return axios({
                method: 'GET',
                url: GET_USER_EQUIP,
                params: params
            })
        },
        queryUser (params) {
            return axios({
                method: 'GET',
                url: API_SEARCH_USER,
                params: params
            })

        },
        addEquips (params) {
            return axios({
                method: 'PUT',
                url: API_ADD_DEVICE,
                data: params
            })
        },
        batchMove (params) {
            return axios({
                method: 'POST',
                url: API_BATCH_MOVE,
                data: params
            })
        },
        singleMove (params) {
            return axios({
                method: 'POST',
                url: API_SINGLE_MOVE,
                data: params
            })
        },
        //编辑
        editEquip (params) {
            return axios({
                method: 'PATCH',
                url: API_EDIT_EQUIP,
                data: params
            })
        },
        //删除
        deleteEquip (id) {
            return axios({
                method: 'DELETE',
                url: API_DELETE_EQUIP + id,
            })
        },
    },
    report: {
        alarmRecord () {
            return axios({
                method: 'GET',
                url: API_GET_ALARM_PROMPT,
            })
        },
        queryByIdType (id, type, params) {
            return axios({
                method: 'GET',
                url: API_QUERY_ALARM_ID + id + '/type/' + type,
                params: params
            })
        },
        queryDealerById (id) {
            return axios({
                method: 'GET',
                url: API_QUERY_DEALER + id,
            })
        },
        queryRecord (params) {
            return axios({
                method: 'GET',
                url: API_QUERY_ALARM,
                params: params
            })
        },
        excelOffline (id, params) {
            return axios({
                method: 'GET',
                url: API_EXCEL_OFFLINE + id,
                params: params
            })
        },
        excelMileageSum (params) {
            return axios({
                method: 'GET',
                url: API_EXCEL_MILEAGE_SUM,
                params: params
            })
        },
        excelDriving (params) {
            return axios({
                method: 'GET',
                url: API_EXCEL_ALARM_CONDITION,
                params: params
            })
        },
        getAllDealer (id) {
            return axios({
                method: 'GET',
                url: API_ALL_DEALER + id,
            })
        }
    },
}

Vue.prototype.$msgDictionary = {
    addSuc: '添加成功！',
    updateSuc: '修改成功！',
    operationSuc: '操作成功！',
    password: {
        required: '密码不能为空',
        diff: '两次输入的新密码不同',
        same: '新旧密码相同',
        pwdFormat: '密码格式不对',
        pwdLength: '密码长度不能大于20'
    },
    imeiError: {
        imeiEmpty: '请输入IMEI号',
        imeiFormat: '请输入大于等于10位的设备号',
        imeiExist: 'IMEI号已经存在',
        name: '设备名不能是纯数字',
    },
    emptyCoordinate: '无坐标信息',
    loginGetaway: '登录过期,请再次登录!',
    loginEmpty: '用户名或密码不能为空！',
    serviceException: '服务器异常,请联系管理员!',
    abnormalPermissions: '权限不符',
    queryEmpty: '未查询到相应数据!',
    email: {
        emptyEmail: '邮箱号必填！',
        errEmail: '邮箱格式错误',
    },
    vCode: {
        emptyVCode: '请输入验证码！',
        errVCode: '验证码错误，请重新获取！',
        sucVCode: '校验成功！',
    },
    addEquips: '请输入添加的设备名，不能为空！',
    noChoose: '请先选择',
    aimEmpty: '目标客户不能为空!',
    equipEmpty: '请选择转移设备!',
    duplication: '不能转移给自己',
    emptyUserName: '请输入客户名称',
    phoneError: '电话号码格式不对',
    timeError: '时间格式不对',
    delFail: '不能删除该客户!请先转移设备后再试!',
    trajectoryEmpty: '请先选择轨迹！',
    registerMsg: {
        name: '用户名仅支持中英文、数字和下划线,且不能超过10个字符',
        emptyName: '用户名必填'
    },
    emptyInfo: '查询条件不能为空!',
    emptyEquipOrGroup: '请先选择设备或组',
    deleteInfo: {
        success: '删除成功',
        failure: '删除失败'
    },
}

Vue.prototype.$globalClick = function (callback) {   //页面全局点击
    $(document).click(callback);
}
